import router from "@/router";
import axios from "axios";
import { ElMessage, ElLoading, LoadingParentElement } from "element-plus";
import { ComponentPublicInstance, ComponentOptionsBase, Ref, inject } from "vue";

const message = inject('message') as any
let loadingInstance: {
  close: any;
  setText?: (text: string) => void;
  removeElLoadingChild?: () => void;
  handleAfterLeave?: () => void;
  vm?: ComponentPublicInstance<
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    false,
    ComponentOptionsBase<
      any,
      any,
      any,
      any,
      any,
      any,
      any,
      any,
      any,
      {},
      {},
      string,
      {}
    >,
    {},
    {}
  >;
  $el?: HTMLElement;
  originalPosition?: Ref<string>;
  originalOverflow?: Ref<string>;
  visible?: Ref<boolean>;
  parent?: Ref<LoadingParentElement>;
  background?: Ref<string>;
  svg?: Ref<string>;
  svgViewBox?: Ref<string>;
  spinner?: Ref<string | boolean>;
  text?: Ref<string>;
  fullscreen?: Ref<boolean>;
  lock?: Ref<boolean>;
  customClass?: Ref<string>;
  target?: Ref<HTMLElement>;
  beforeClose?: Ref<(() => boolean) | undefined> | undefined;
  closed?: Ref<(() => void) | undefined> | undefined;
};
axios.defaults.baseURL = "/seller"; //测试
// const dialogVisible = ref(false);
//axios.defaults.baseURL = process.env.VUE_APP_BASE_API;
//post请求头
//axios.defaults.headers.post["token"] = "22222";
//允许跨域携带cookie信息
axios.defaults.withCredentials = true;
//设置超时
// axios.defaults.timeout = 15000;

// 请求拦截
axios.interceptors.request.use(
  (config) => {
    let url = config.url;
    if (url != "home/getBusiness") {
      loadingInstance = ElLoading.service({ text: "加载中" });
    }

    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截
axios.interceptors.response.use(
  (response) => {
    if (response) {
      if (response.data) {
        if (response.data.code == 4) {
          ElMessage({
            message: response.data.msg, type: "warning"
          },);
          router.push("/index/login");

        }
      }

    }

    loadingInstance.close();
    return Promise.resolve(response.data);
  },
  (error) => {
    message.warning(error + '请刷新重试')
    return Promise.reject(error);
  }
);
export default axios;
// export default {
//   /**
//    * @param {String} url
//    * @param {Object} data
//    * @returns Promise
//    */
//   post(url: string, data: any) {
//     return new Promise((resolve, reject) => {
//       axios({
//         method: "post",
//         url,
//         data: qs.parse(data),
//       })
//         .then((res) => {
//           resolve(res);
//         })
//         .catch((err) => {
//           reject(err);
//         });
//     });
//   },

//   get(url: string, data: any) {
//     return new Promise((resolve, reject) => {
//       axios({
//         method: "get",
//         url,
//         params: data,
//       })
//         .then((res) => {
//           resolve(res.data);
//         })
//         .catch((err) => {
//           reject(err);
//         });
//     });
//   },
// };
